import React, { useState, useEffect } from 'react';
import { Table, Tag, Button, message, Spin, Card } from 'antd';
import { EyeOutlined, ReloadOutlined } from '@ant-design/icons';
import { judgeApi } from '../../api/services/Judge/judgeApi';
import type {  QueryJudgeInfo } from '../../api/dto/judge';
import type { JudgeStatusListVO } from '../../api/vo/judge';
import type { JudgeStatusListResponse } from '../../api/vo/judge';
import './SubmissionList.less';

interface SubmissionListProps {
  userId?: number;
  problemId?: number;
  onViewSubmission?: (submitId: number) => void;
}

const SubmissionList: React.FC<SubmissionListProps> = ({ 
  userId = 1, 
  problemId,
  onViewSubmission 
}) => {
  const [loading, setLoading] = useState(false);
  const [submissions, setSubmissions] = useState<JudgeStatusListVO[]>([]);
  const [total, setTotal] = useState(0);
  const [currentPage, setCurrent] = useState(1);
  const [pageSize, setPageSize] = useState(10);

  // 获取提交记录
  const fetchSubmissions = async (page = 1, size = 10) => {
    setLoading(true);
    try {
      const queryData: QueryJudgeInfo = {
        page: page,
        size: size,
        userId: userId,
        pid:problemId,
      };
      
      const response: JudgeStatusListResponse = await judgeApi.getJudgeStatusList(queryData);
      
      if (response.code === 200 && response.data) {
        setSubmissions(response.data.records || []);
        setTotal(response.data.total || 0);
        setCurrent(page);
        setPageSize(size);
      } else {
        message.error(response.message || '获取提交记录失败');
      }
    } catch (error) {
      console.error('获取提交记录失败:', error);
      message.error('获取提交记录失败');
    } finally {
      setLoading(false);
    }
  };

  // 组件挂载时获取数据
  useEffect(() => {
    fetchSubmissions();
  }, [userId, problemId]);


  // 渲染判题结果标签
  const renderJudgeResult = (result: number) => {
    const resultMap: Record<number, { text: string; color: string }> = {
      [-1]: { text: '失败', color: '#f5222d' },
      [0]: { text: 'CE', color: '#fa8c16' },
      [1]: { text: 'AC', color: '#52c41a' },
      [2]: { text: 'WA', color: '#f5222d' },
      [3]: { text: 'TLE', color: '#fa541c' },
      [4]: { text: 'MLE', color: '#722ed1' },
      [5]: { text: 'PE', color: '#13c2c2' },
      [6]: { text: 'RE', color: '#eb2f96' },
      [7]: { text: 'SE', color: '#595959' }
    };
    
    const resultInfo = resultMap[result] || { text: '未知', color: '#d9d9d9' };
    return <Tag color={resultInfo.color}>{resultInfo.text}</Tag>;
  };

  // 渲染语言标签
  const renderLanguage = (language: string) => {
    const languageColors: Record<string, string> = {
      'JAVA': '#f56a00',
      'CPP': '#2f54eb',
      'C': '#722ed1',
      'PYTHON': '#52c41a',
      'JAVASCRIPT': '#faad14'
    };
    
    return (
      <Tag color={languageColors[language] || '#108ee9'}>
        {language}
      </Tag>
    );
  };

  // 表格列定义
  const columns = [

    {
      title: '判题结果',
      dataIndex: 'judgeResult',
      key: 'judgeResult',
      width: 80,
      render: renderJudgeResult
    },
    {
      title: '时间',
      dataIndex: 'judgeTime',
      key: 'judgeTime',
      width: 70,
      render: (time: number) => `${time || 0}ms`,
    },
    {
      title: '内存',
      dataIndex: 'judgeMemory',
      key: 'judgeMemory',
      width: 70,
      render: (memory: number) => `${memory || 0}KB`,
    },
    {
      title: '语言',
      dataIndex: 'language',
      key: 'language',
      width: 70,
      render: renderLanguage
    },
    {
      title: '提交时间',
      dataIndex: 'createTime',
      key: 'createTime',
      width: 100,
      render: (time: string) => new Date(time).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      }),
    },
 
  ];

  return (
      <Table
        columns={columns}
        dataSource={submissions}
        rowKey="submitId"
        loading={loading}
        pagination={{
          current: currentPage,
          pageSize: pageSize,
          total: total,
          showSizeChanger: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
          onChange: (page, size) => {
            fetchSubmissions(page, size || pageSize);
          },
          onShowSizeChange: (current, size) => {
            fetchSubmissions(1, size);
          }
        }}
      />
  );
};

export default SubmissionList;